package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.saleDao;
import com.hdaccp.xingmu.dao.DanyuanDao;
import com.hdaccp.xingmu.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sale")
public class SaleConn {
    @PersistenceContext
    private EntityManager entityManager ;
    @Autowired
    private saleDao saleDao;
    @Autowired
    private DanyuanDao danyuanDao;
    @RequestMapping("/tiao")
    public String tiao(){
        return "WEB-INF/jsp/ruchang";
    }

    @RequestMapping("/tiao2")
    public String tiao2(){
        return "WEB-INF/jsp/index";
    }

    @RequestMapping("/tiao3")
    public String tiao3(){
        return "WEB-INF/jsp/ruzhu";
    }


    @RequestMapping("/tree")
    @ResponseBody
    public List<tree> ll(){
        List<Sale> list = saleDao.findAll();
        List<Danyuan> list2 = danyuanDao.findAll();

        List<tree> t  = new ArrayList<>();

        for (Sale s:list){
            tree t1 = new tree();
            t1.setId(s.getId());
            t1.setText(s.getName());
            List<tree> t3 = new ArrayList<>();
            for(Danyuan d:list2){
                if(s.getId() == d.getLoupan()){
                    tree t2 = new tree();
                    t2.setId(d.getId());
                    t2.setText(d.getName());
                    t3.add(t2);
                }
            }
            t1.setChildren(t3);
            t.add(t1);
        }
        return t;

    }
  /*  @RequestMapping("/finl")
    @ResponseBody
    public List<Sale> listaa(){
        return saleDao.findAll();
    }*/
    //查询全部
    @RequestMapping("/quanbujianyi")
    @ResponseBody
    public Map<String,Object> quanbu(int pageSize,int pageNumber) {
        //分页
        String jpal = "from Sale where 1=1";

        Query qu = entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((pageNumber - 1) * pageSize);
        //结束页数
        qu.setMaxResults(pageSize);

        //查询多少条数据
        String jpa = "select count(s) from Sale s";

        Query q = entityManager.createQuery(jpa);

        Long count = (Long) q.getSingleResult();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", qu.getResultList());
        return map;
    }

    @RequestMapping("/tianjia")
    @ResponseBody
    public Map<String,Object> add(Sale sale)throws UnsupportedEncodingException {

        saleDao.save(sale);
        return quanbu(4,1);
    }
    @RequestMapping("/shanc")
    @ResponseBody
    public Map<String,Object> shanc(Long id){
        saleDao.deleteById(id);
        return quanbu(4,1);
    }
    @RequestMapping("/xiugai")
    @ResponseBody
    public Map<String,Object> upd(Sale sale)throws UnsupportedEncodingException{
        saleDao.save(sale);
        return quanbu(4,1);
    }
    @RequestMapping("/ld")
    @ResponseBody
    public List<Sale> lsiy(){
        return saleDao.findAll();
    }

}
